package com.example.config;

import com.example.constant.UserStatus2;
import com.example.conventor.UserStatusConverter;
import com.example.conventor.UserStatusEnumSerializer;
import com.example.day03.ConstantEnumConverterFactory;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author jeff
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new UserStatusConverter());


    }


//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(diyConvert());
//    }
//
//
//
//    /**
//     * 自定义消息转换器
//     * @Bean 标记该方法返回一个由Spring管理的Bean对象
//     * @return
//     */
//    @Bean
//    public MappingJackson2HttpMessageConverter diyConvert() {
//        MappingJackson2HttpMessageConverter mappingJackson2CborHttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        //用于JSON 数据的序列化和反序列化
//
//        SimpleModule simpleModule = new SimpleModule();
////        simpleModule.addSerializer(LocalDateTime.class, new DiyLocalDatetimeSerializer());
//        simpleModule.addSerializer(UserStatus2.class, new UserStatusEnumSerializer());
//
//        //添加针对 LocalDateTime 类型的自定义序列化器 DiyLocalDatetimeSerializer
//
//        objectMapper.registerModule(simpleModule);
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        //设置JSON 数据中包含目标对象中不存在的属性时，直接忽略这些属性而不是中断反序列化过程。
//
//        mappingJackson2CborHttpMessageConverter.setObjectMapper(objectMapper);
//        return mappingJackson2CborHttpMessageConverter;
//
//    }
}
